/*
    nspire-gamekit - a simple homebrew game framework
    Copyright (C) 2011  Daniel Tang

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "ndlesscpp.h"
#include "gameloop.h"
#include "types/gameloop.h"


extern "C" {
    #include <os.h>
}
#include "screen.h"
#include "nimg.h"
#include "gameloop.h"
#include "file.h"
#include "types/gameloop.h"
#include "types/timer.h"
#include "types/ngk.h"
#include "debug.h"
//#include "ndlesscpp.h"

class GamePlane;
class PlaneObj;

#define gravity 0.5;

class PlaneObj {
    private:
        NGKScreenBuffer* buffer;
        
        float velocity;
        NGKTimer<PlaneObj> *timer;
    public:
        NGKPoint position;
        
        PlaneObj() {
            buffer = screenBufferFromFile("plane.tns");
            position = NGKPointMake(60,60);
            velocity = 0.0f;
            
            timer = new NGKTimer<PlaneObj>(17, this, &PlaneObj::update);
            timer->start();
        }
        ~PlaneObj() {
            screenBufferDestroy(buffer);
            delete timer;
        }
        void draw(NGKScreenBuffer* frameBuffer) {
            if (buffer) screenBufferDrawScreen(position,buffer,frameBuffer);
        }
        void update(NGKGameTime time) {
            if (isKeyPressed(KEY_NSPIRE_2)) {
                velocity -= gravity;
            }else{
                velocity += gravity;
            }
            position.y += velocity;
    
            if (position.y > SCREEN_HEIGHT || position.y < -screenBufferHeight(buffer)) {
            }
        }

};

class GamePlane : public NGKGame {
    private:
        NGKScreenBuffer* frameBuffer;
        NGKScreenBuffer* mount;
        NGKScreenBuffer* title;
        
        PlaneObj *plane;
        
        char bammed;
        int score;
        
        NGKTimer<GamePlane> *timer;
        NGKTimer<GamePlane> *drawTimer;
    public:
        GamePlane() {
            frameBuffer = NULL;
            plane = NULL;
            mount = NULL;
            title = NULL;
        }
        ~GamePlane() {
            screenBufferDestroy(frameBuffer);
            screenBufferDestroy(mount);
            screenBufferDestroy(title);
            delete plane;
            delete timer;
            delete drawTimer;
        }
        int init() {
            frameBuffer = screenBufferAlloc(NGKSizeMake(320, 240));
            screenBufferClear(255, frameBuffer);
            mount = screenBufferFromFile("mount.tns");
            title = screenBufferFromFile("header.tns");
            if (!frameBuffer) { printf("Failed to alloc\n"); return -1; }
            if (!mount) { printf("Failed to load from file\n"); return -1; }
            
            timer = new NGKTimer<GamePlane>(17, this, &GamePlane::update);
            timer->start();
            
            drawTimer = new NGKTimer<GamePlane>(17, this, &GamePlane::draw);
            drawTimer->start();
            
            plane = new PlaneObj();

            return 0;
            
        }
        void bam() {
            plane->position.y = 60;
        }
        void update(NGKGameTime time) {
            if (isKeyPressed(KEY_NSPIRE_5)) { gameLoopExit(0); return; }
            score++;
        }
        void draw(NGKGameTime time) {
            plane->draw(frameBuffer);
            screenBufferFlushToScreen(frameBuffer);
        }
};





int main(int argc, char *argv[]) {
    NGKGame * game = new GamePlane();
    gameSetCurrentDir(argv[0]);
    gameLoopRun(static_cast<NGKGame*>(game),0);
    gameRestoreCurrentDir();
    return 0;
}